package com.airlines.crewassignment.service.crew.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.airlines.crewassignment.dao.CrewFlightExperienceAcTypeMapper;
import com.airlines.crewassignment.dao.CrewMapper;
import com.airlines.crewassignment.domain.Crew;
import com.airlines.crewassignment.domain.CrewFlightExperienceAcType;
import com.airlines.crewassignment.domain.DateTimeRange;
import com.airlines.crewassignment.entity.crew.CrewExperienceAcTypeValue;
import com.airlines.crewassignment.service.crew.CrewFlightExperienceAcTypeService;

/**
 * @author xxxx
 * @date 2018年5月21日 
 * @description
 */
@Service
public class CrewFlightExperienceAcTypeServiceImpl implements CrewFlightExperienceAcTypeService{
	@Autowired
	private  CrewFlightExperienceAcTypeMapper cfeAcTypeMapper;
	@Autowired
	private CrewMapper crewMapper;
	@Override
	public List<CrewFlightExperienceAcType> crewFlightExperienceAcTypeListInit(String dutyCode, Date beforeFlightDate,
			Date endFlightDate) {
		
		//1.初始化数据结构
		List<CrewFlightExperienceAcType> cfeaList = new ArrayList<>();
		List<Crew> crewList = crewMapper.listCrewBase(dutyCode, null);
		List<String> acTypeList = cfeAcTypeMapper.listAcType();
		for(Crew c : crewList){
			for(String acType : acTypeList){
				CrewFlightExperienceAcType cfea = new CrewFlightExperienceAcType();
				cfea.setWorkcode(c.getWorkcode());
				cfea.setAcType(acType);
				cfea.setEndDate(beforeFlightDate);			
				cfeaList.add(cfea);
			}
		}
		
		//2.起落次数初始化
		this.updownCount90DaysInit(cfeaList,dutyCode, beforeFlightDate, endFlightDate);
		
		//3.左座所有经历初始化
		this.leftDurationInit(cfeaList,dutyCode, beforeFlightDate, endFlightDate);
		
		//4.左座近90天经历初始化
		this.leftDurationLast90DaysInit(cfeaList,dutyCode, beforeFlightDate, endFlightDate);
		
		//5.右座经历初始化
		this.rightDurationInit(cfeaList,dutyCode, beforeFlightDate, endFlightDate);
		
		//6.右座近90天经历初始化
		this.rightDurationLast90DaysInit(cfeaList,dutyCode, beforeFlightDate, endFlightDate);
		
		//7.经历初始化
		this.seatDurationInit(cfeaList,dutyCode, beforeFlightDate, endFlightDate);
		
		//7.近90天经历初始化
		this.seatDurationLast90DaysInit(cfeaList,dutyCode, beforeFlightDate, endFlightDate);
		
		return cfeaList;
	}
	

	/**
	 * 近90天起落次数初始化
	 * @param dutyCode
	 * @param beforeFlightDate
	 * @param endFlightDate
	 */
	private void updownCount90DaysInit(List<CrewFlightExperienceAcType> cfeaList,String dutyCode, Date beforeFlightDate, Date endFlightDate) {
		Date startDate = new DateTime(beforeFlightDate).minusDays(90).toDate();
		Date endDate = new DateTime(beforeFlightDate).minusDays(1).toDate();
		DateTimeRange dateTimeRange = new DateTimeRange(startDate,endDate);
		List<CrewExperienceAcTypeValue> list = cfeAcTypeMapper.listUpdownCount(dutyCode, null, dateTimeRange);
		for(CrewFlightExperienceAcType cfea : cfeaList){
			for(CrewExperienceAcTypeValue cv: list){
				if(crewFlightExperienceAcTypeEquals(cfea,cv)){
					cfea.setUpdownCount90Days(cv.getValueInt());
				}
			}
		}
		
	}
	
	/**
	 * 判断机型经历类，与机型经历指标值是否匹配
	 * @param cfea
	 * @param value
	 * @return
	 */
	private boolean crewFlightExperienceAcTypeEquals(CrewFlightExperienceAcType cfea , CrewExperienceAcTypeValue cv){
		return StringUtils.equals(cfea.getWorkcode(), cv.getWorkcode()) && StringUtils.equals(cfea.getAcType(), cv.getAcType());
	}
	
	/**
	 * 左座经历初始化
	 * @param cfeaList
	 * @param dutyCode
	 * @param beforeFlightDate
	 * @param endFlightDate
	 */
	private void leftDurationInit(List<CrewFlightExperienceAcType> cfeaList, String dutyCode, Date beforeFlightDate,
			Date endFlightDate) {
		DateTime startDate = new DateTime(2000,1,1,1,1);
		Date endDate = new DateTime(beforeFlightDate).minusDays(1).toDate();
		DateTimeRange dateTimeRange = new DateTimeRange(startDate.toDate(),endDate);
		List<CrewExperienceAcTypeValue> list  = cfeAcTypeMapper.leftDuration(dutyCode, null, dateTimeRange);
		
		for(CrewFlightExperienceAcType cfea : cfeaList){
			for(CrewExperienceAcTypeValue cv: list){
				if(crewFlightExperienceAcTypeEquals(cfea,cv)){
					cfea.setLeftDuration(cv.getValueInt());
				}
			}
		}
	}
	
	/**
	 * 左座近90天经历初始化
	 * @param cfeaList
	 * @param dutyCode
	 * @param beforeFlightDate
	 * @param endFlightDate
	 */
	private void leftDurationLast90DaysInit(List<CrewFlightExperienceAcType> cfeaList, String dutyCode,
			Date beforeFlightDate, Date endFlightDate) {
		Date startDate = new DateTime(beforeFlightDate).minusDays(90).toDate();
		Date endDate = new DateTime(beforeFlightDate).minusDays(1).toDate();
		DateTimeRange dateTimeRange = new DateTimeRange(startDate,endDate);
		List<CrewExperienceAcTypeValue> list  = cfeAcTypeMapper.leftDuration(dutyCode, null, dateTimeRange);
		
		for(CrewFlightExperienceAcType cfea : cfeaList){
			for(CrewExperienceAcTypeValue cv: list){
				if(crewFlightExperienceAcTypeEquals(cfea,cv)){
					cfea.setLeftDurationLast90Days(cv.getValueInt());
				}
			}
		}	
	}
	
	/**
	 * 右座经历初始化
	 * @param cfeaList
	 * @param dutyCode
	 * @param beforeFlightDate
	 * @param endFlightDate
	 */
	private void rightDurationInit(List<CrewFlightExperienceAcType> cfeaList, String dutyCode, Date beforeFlightDate,
			Date endFlightDate) {
		DateTime startDate = new DateTime(2000,1,1,1,1);
		Date endDate = new DateTime(beforeFlightDate).minusDays(1).toDate();
		DateTimeRange dateTimeRange = new DateTimeRange(startDate.toDate(),endDate);
		List<CrewExperienceAcTypeValue> list  = cfeAcTypeMapper.rightDuration(dutyCode, null, dateTimeRange);
		
		for(CrewFlightExperienceAcType cfea : cfeaList){
			for(CrewExperienceAcTypeValue cv: list){
				if(crewFlightExperienceAcTypeEquals(cfea,cv)){
					cfea.setRightDuration(cv.getValueInt());
				}
			}
		}
		
	}
	/**
	 * 右座近90天经历初始化
	 * @param cfeaList
	 * @param dutyCode
	 * @param beforeFlightDate
	 * @param endFlightDate
	 */
	private void rightDurationLast90DaysInit(List<CrewFlightExperienceAcType> cfeaList, String dutyCode,
			Date beforeFlightDate, Date endFlightDate) {
		Date startDate = new DateTime(beforeFlightDate).minusDays(90).toDate();
		Date endDate = new DateTime(beforeFlightDate).minusDays(1).toDate();
		DateTimeRange dateTimeRange = new DateTimeRange(startDate,endDate);
		List<CrewExperienceAcTypeValue> list  = cfeAcTypeMapper.rightDuration(dutyCode, null, dateTimeRange);
		
		for(CrewFlightExperienceAcType cfea : cfeaList){
			for(CrewExperienceAcTypeValue cv: list){
				if(crewFlightExperienceAcTypeEquals(cfea,cv)){
					cfea.setRightDurationLast90Days(cv.getValueInt());
				}
			}
		}	
		
	}
	
	
	

	/**
	 * 经历时间初始化
	 * @param cfeaList
	 * @param dutyCode
	 * @param beforeFlightDate
	 * @param endFlightDate
	 */
	private void seatDurationInit(List<CrewFlightExperienceAcType> cfeaList, String dutyCode, Date beforeFlightDate,
			Date endFlightDate) {
		DateTime startDate = new DateTime(2000,1,1,1,1);
		Date endDate = new DateTime(beforeFlightDate).minusDays(1).toDate();
		DateTimeRange dateTimeRange = new DateTimeRange(startDate.toDate(),endDate);
		List<CrewExperienceAcTypeValue> list  = cfeAcTypeMapper.seatDuration(dutyCode, null, dateTimeRange);
		
		for(CrewFlightExperienceAcType cfea : cfeaList){
			for(CrewExperienceAcTypeValue cv: list){
				if(crewFlightExperienceAcTypeEquals(cfea,cv)){
					cfea.setSeatDuration(cv.getValueInt());
				}
			}
		}
		
	}
	
	/**
	 * 近90天经历初始化
	 * @param cfeaList
	 * @param dutyCode
	 * @param beforeFlightDate
	 * @param endFlightDate
	 */
	private void seatDurationLast90DaysInit(List<CrewFlightExperienceAcType> cfeaList, String dutyCode, Date beforeFlightDate,
			Date endFlightDate) {
		Date startDate = new DateTime(beforeFlightDate).minusDays(90).toDate();
		Date endDate = new DateTime(beforeFlightDate).minusDays(1).toDate();
		DateTimeRange dateTimeRange = new DateTimeRange(startDate,endDate);
		List<CrewExperienceAcTypeValue> list  = cfeAcTypeMapper.seatDuration(dutyCode, null, dateTimeRange);
		
		for(CrewFlightExperienceAcType cfea : cfeaList){
			for(CrewExperienceAcTypeValue cv: list){
				if(crewFlightExperienceAcTypeEquals(cfea,cv)){
					cfea.setSeatDurationLast90Days(cv.getValueInt());
				}
			}
		}
		
	}

}
